package com.aotain.oframework.dao.sqlt;

import javax.persistence.EntityManager;

import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
import org.springframework.data.repository.core.RepositoryMetadata;

public class JpaSqltRepositoryFactory extends JpaRepositoryFactory {
	private EntityManager entityManager;

	public JpaSqltRepositoryFactory(EntityManager entityManager) {
		super(entityManager);
		this.entityManager = entityManager;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected Object getTargetRepository(RepositoryMetadata metadata) {
		if (JpaSqltRepository.class.isAssignableFrom(metadata.getRepositoryInterface())) {
			return new JpaSqltRepositoryImpl(metadata.getDomainType(), entityManager);
		} else {
			return super.getTargetRepository(metadata, entityManager);
		}
	}

	@Override
	protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
		if (JpaSqltRepository.class.isAssignableFrom(metadata.getRepositoryInterface())) {
			return JpaSqltRepositoryImpl.class;
		} else {
			return super.getRepositoryBaseClass(metadata);
		}
	}

}